home *** CD-ROM | disk | FTP | other *** search
- { uabort.pas -- Printer support unit with abort dialog }
-
- unit UAbort;
-
- interface
-
- {$R uabort.res}
-
- uses WinTypes, WinProcs, WObjects, Strings;
-
- const
-
- abortID = 'ABORTDIAG'; { Dialog resource ID }
-
- type
-
- PAbort = ^TAbort;
- TAbort = object(TDlgWindow)
- procedure WMInitDialog(var Msg: TMessage);
- virtual wm_First + wm_InitDialog;
- procedure WMCommand(var Msg: TMessage);
- virtual wm_First + wm_Command;
- end;
-
- var
-
- PDc: HDC; { Printer's DC: valid if PrnStart = true }
- Printing: Boolean; { True after successful call to PrnStart }
- EscResult: Integer; { Result of most recent call to Escape }
- Aborted: Bool; { True if Cancel button selected }
-
-
- function NextToken(P: PChar; C: Char): PChar;
- function PrnStart(DocumentName: PChar): Boolean;
- procedure NewPage;
- procedure PrnStop;
-
-
- implementation
-
- var
-
- AbortDiag: PAbort; { Pointer to abort dialog object }
- HAbortDiag: HWnd; { Handle to modeless abort dialog }
- PAbortProc: TFarProc; { Pointer to abort callback function }
-
-
- {- Return pointer to next token in P or previous P if P = nil }
- function NextToken(P: PChar; C: Char): PChar;
- const
- Next: PChar = nil;
- begin
- if P = nil then P := Next;
- Next := StrScan(P, C);
- if Next <> nil then
- begin
- Next^ := #0;
- Next := @Next[1]
- end;
- NextToken := P
- end;
-
- {- Abort callback function }
- function AbortProc(PDc: HDC; Code: Integer): Bool; export;
- var
- Msg: TMsg;
- begin
- while (not Aborted) and PeekMessage(Msg, 0, 0, 0, pm_Remove) do
- if (HAbortDiag = 0) or not IsDialogMessage(HAbortDiag, Msg) then
- begin
- TranslateMessage(Msg);
- DispatchMessage(Msg)
- end;
- AbortProc := not Aborted
- end;
-
- {- If true, printing has been initialized }
- function PrnStart(DocumentName: PChar): Boolean;
- var
- Buffer: array[0 .. 80] of Char;
- DriverName, DeviceName, OutputName: PChar;
- begin
- GetProfileString('windows', 'device', ',,', Buffer, Sizeof(Buffer));
- DeviceName := NextToken(Buffer, ',');
- DriverName := NextToken(nil, ',');
- OutputName := NextToken(nil, ',');
- Aborted := false;
- PDc := CreateDC(DriverName, DeviceName, OutputName, nil);
- if PDc <> 0 then
- begin
- AbortDiag := PAbort(Application^.MakeWindow(
- New(PAbort, Init(Application^.MainWindow, abortID))));
- if AbortDiag = nil then
- begin
- Application^.Error(em_OutOfMemory);
- Printing := false
- end else
- begin
- HAbortDiag := AbortDiag^.HWindow;
- PAbortProc := MakeProcInstance(@AbortProc, HInstance);
- EscResult := Escape(PDc, SetAbortProc, 0, PAbortProc, nil);
- if EscResult >= 0 then
- EscResult := Escape(PDc, StartDoc, StrLen(DocumentName),
- DocumentName, nil);
- Printing := EscResult > 0
- end
- end;
- if not Printing then
- begin
- if AbortDiag <> nil then
- AbortDiag^.CloseWindow;
- MessageBox(Application^.MainWindow^.HWindow,
- 'Printer initialization failed', 'Error',
- mb_IconExclamation or mb_Ok)
- end;
- PrnStart := Printing
- end;
-
- {- Print current page and start a new one }
- procedure NewPage;
- begin
- if Printing and (EscResult > 0) then
- EscResult := Escape(PDc, NewFrame, 0, nil, nil)
- end;
-
- {- Call only if PrnStop returned true. }
- procedure PrnStop;
- begin
- if Printing then
- begin
- if EscResult > 0 then
- Escape(PDc, EndDoc, 0, nil, nil);
- if AbortDiag <> nil then
- AbortDiag^.CloseWindow;
- DeleteDC(PDc);
- Printing := false
- end
- end;
-
-
- { TAbort }
-
- procedure TAbort.WMInitDialog(var Msg: TMessage);
- begin
- SetFocus(HWindow)
- end;
-
- procedure TAbort.WMCommand(var Msg: TMessage);
- begin
- Aborted := true
- end;
-
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 5/19/1991
- ---------------------------------------------------------------}
-